<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" width="800px"
    top="5vh" @close="clearDataList()" append-to-body>
    <el-container>
      <el-main style="max-height:72vh">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
          label-width="150px">
          <!-- <el-form-item label="履历年份" prop="llYear">
            <el-date-picker v-model="dataForm.llYear" placeholder="履历年份" style="width:90%;"
                            :picker-options="pickerOptions0" format="yyyy" value-format="yyyy" type="year" ></el-date-picker>
          </el-form-item> -->
          <el-row>
            <el-col  :span="18">
              <el-form-item label="姓名" prop="ryXm">
                <!--            <el-input :readonly="dataForm.id ? true : false" v-model="dataForm.ryXm" placeholder="姓名"></el-input>-->

                <el-input v-model="dataForm.ryXm" :disabled="true" placeholder="请选择"/>

              </el-form-item>
            </el-col>
            <el-col  :span="6">
              &nbsp;<el-button @click="selectCheckUser" :disabled="dataForm.id ? true : false">选择</el-button>
            </el-col>
          </el-row>


          <el-form-item label="开始日期" prop="llKssj" ref="rLlKssj">
            <el-date-picker v-model="dataForm.llKssj" placeholder="开始日期" style="width:90%;" @blur='jisuan' value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date"></el-date-picker>
          </el-form-item>
          <el-form-item label="结束日期" prop="llJssj" ref="rLlJssj">
            <el-date-picker v-model="dataForm.llJssj" placeholder="结束日期" style="width:90%;" @blur='jisuan' value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date"></el-date-picker>
          </el-form-item>
          <el-form-item label="工作单位" prop="ryGzdw">
            <avue-input-tree v-model="dataForm.ryGzdwId" :node-click="getNodeDataGzdw" :dic="treeDeptData"
                             :props="defaultProps" placeholder="工作单位" class="wbf90" />

          </el-form-item>
          <el-form-item label="职务或工种" prop="ryZwgz">
            <el-input v-model="dataForm.ryZwgz" placeholder="职务或工种" class="wbf90"></el-input>
          </el-form-item>
          <el-form-item label="职务级别" prop="zwjb">
             <el-select v-model="dataForm.zwjb" placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
          </el-form-item>
          <el-form-item label="行政级别" prop="ryXzjb">
            <!-- <el-select :clearable="true" v-model="dataForm.ryXzjbBh" @change="elChange" placeholder="请选择" class="xiaLa wbf90">
              <el-option v-for="(item,index) in userZjOptions " :key="index" :label="item.label" :value="item.value">
              </el-option>
            </el-select> -->
            <el-select :clearable="true" v-model="dataForm.ryXzjbBh" @change="elChangeTwo" placeholder="请选择" class="xiaLa wbf90">
              <el-option v-for="(item,index) in userZjOptionsTwo " :key="index" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

          <!-- <el-form-item label="奖金发放部门" prop="jjFfDeptName">
            <avue-input-tree v-model="dataForm.jjFfDeptId" :node-click="getNodeDatajjFfBm" :dic="treeDeptData"
                             :props="defaultProps" placeholder="奖金发放部门" class="wbf90" />

          </el-form-item> -->

          <el-form-item label="任职时长" prop="jjFfMonth">
            <el-input v-model="dataForm.jjFfMonth" readonly label="描述文字" class="wbf90"></el-input>
          </el-form-item>

          <!-- <el-form-item label="技术职称" prop="ryJszc">
            <el-input v-model="dataForm.ryJszc" placeholder="技术职称" class="wbf90"></el-input>
          </el-form-item>

          <el-form-item label="技能级别" prop="ryJnjb">
            <el-input v-model="dataForm.ryJnjb" placeholder="技能级别" class="wbf90"></el-input>
          </el-form-item> -->
          
          <el-form-item v-if="dataForm.ryXzjb.includes('正')" label="绩效年薪基数">
            <el-input type="number" v-model="dataForm.jxNxjs" placeholder="绩效年薪基数" class="wbf90"></el-input>
          </el-form-item>

          <el-form-item  v-if="dataForm.ryXzjb.includes('副')" label="副职年薪标准(%)">
            <el-input type="number" v-model="dataForm.fzNxbz" placeholder="副职年薪标准" class="wbf90"></el-input>
          </el-form-item>

          <el-form-item label="备注" prop="llBz">
            <el-input v-model="dataForm.llBz" placeholder="备注" rows="2"  type="textarea" class="wbf90"></el-input>
          </el-form-item>

        </el-form>
      </el-main>
    </el-container>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button @click="clearDataList()" type="primary" plain>取消</el-button>
    </span>
    <!-- 人员选择 -->
    <check-user ref="checkUser" :multiple='false' :roleCode="roleCode" :userIds='checkUserIds' @getUserList="getUserCheck"></check-user>
  </el-dialog>
</template>

<script>
  import { getObj, addObj, putObj,returnDouble} from '@/api/jiangjin/jiangjinryllb'
  import {fetchTree,getTreeList} from '@/api/admin/dept'
  import {remote as remoteDict } from '@/api/admin/dict'
  import {getJiangjinRyxxbPerPage} from '@/api/jiangjin/jiangjinryxxb'
  import checkUser from '@/components/form/compound/selectRyxx'

  export default {
    components:{
      checkUser
    },
    data() {
      var validateKssj = (rule, value, callback) => {
        if(this.dataForm.llJssj !== "" && this.dataForm.llJssj != null && value !==""){
          if(value > this.dataForm.llJssj){
            callback(new Error("开始时间应小于结束时间！"));
          }else {
            this.$refs["rLlJssj"].clearValidate();
            callback();
          }
        }else {
          callback();
        }
      };
      var validateJssj = (rule, value, callback) => {
        if(this.dataForm.llKssj !== "" && this.dataForm.llKssj != null && value !==""){
          if( this.dataForm.llKssj > value){
            callback(new Error("结束时间应大于开始时间！"));
          }else {
            this.$refs["rLlKssj"].clearValidate();
            callback();
          }
        }else {
          callback();
        }
      };
      return {
        pickerOptions0: {
          disabledDate(time) {
            return time.getTime() > Date.now() - 8.64e6
          }
        },
        visible: false,
        canSubmit: false,
        checkUserIds:[],
        roleCode:'',
        dataForm: {
            id: 0,
          ryCode: '',
          ryXm: '',
          llKssj: '',
          llJssj: '',
          ryGzdwId:'',
          ryGzdw: '',
          ryGzdwCode: '',
          zwjb:"",
          ryZwgz: '',
          ryXzjb: '',
          ryXzjbBh:'',
          ryJszc: '',
          ryJnjb: '',
          llBz: '',
          llYear:'',
          jjFfDeptId:'',
          jjFfDeptCode:'',
          jjFfDeptName:'',
          jjFfMonth:'',
          rglrBs:'',
          ryXXData:[],
          deptId:''
        },

        userZjOptions:[],
        treeDeptData: [],
        defaultProps: {
          label: 'name',
          value: 'id'
        },
        dataRule: {
          llYear: [
            { required: true, message: '履历年份不能为空', trigger: 'blur' }
          ],
          ryXm: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          llKssj: [
            { required: true, message: '开始日期不能为空', trigger: 'blur' },
            { validator: validateKssj, trigger: 'blur' }
          ],
          llJssj: [
            { required: true, message: '结束日期不能为空', trigger: 'blur' }
            ,{ validator: validateJssj, trigger: 'blur' }
          ],
          ryGzdw: [
            { required: true, message: '工作单位不能为空', trigger: 'blur' }
          ],
          zwjb:[
            {required: true, message: '职务级别不能为空', trigger: 'blur' }
          ],
          jjFfDeptName: [
            { required: true, message: '奖金发放部门不能为空', trigger: 'blur' }
          ],
          ryZwgz: [
            { required: true, message: '职务或工种不能为空', trigger: 'blur' },
            { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
          ],
          ryXzjb: [
            { required: true, message: '行政级别不能为空', trigger: 'blur' }
          ],
          ryJszc:[
            { min: 0, max: 50, message: '长度最大50个字符', trigger: 'blur' }
          ]
          ,
          ryJnjb:[
            { min: 0, max: 50, message: '长度最大50个字符', trigger: 'blur' }
          ],
          llBz:[
            { min: 0, max: 250, message: '长度最大250个字符', trigger: 'blur' }
          ],
          
        },
        renzhi:"31",
        options:[
          {
          value: '正处级',
          label: '正处级'},
          {
          value: '副处级',
          label: '副处级'},
          {
          value: '副局级',
          label: '副局级'},
        ],
        userZjOptionsTwo:[
          {
          value: '领导正职',
          label: '领导正职'},
          {
          value: '领导副职',
          label: '领导副职'},
          {
          value: '副总师',
          label: '副总师'},
        ]
      }
    },
    created() {
      fetchTree().then(response => {
        this.treeDeptData = response.data.data
        ////console.log(this.treeDeptData);
      })
      remoteDict("user_zj").then(response => {
        this.userZjOptions = response.data.data
      })

    },
    methods: {
      jisuan(){
        if(this.dataForm.llKssj&&this.dataForm.llJssj){
          var yearOne=this.dataForm.llKssj.substring(0,4)
          var yearTwo=this.dataForm.llJssj.substring(0,4)
          var monthOne,monthTwo,dateOne,dateTwo;
          if(this.dataForm.llKssj.substring(5,6)==0){
              monthOne=this.dataForm.llKssj.substring(6,7)
          }else{
              monthOne=this.dataForm.llKssj.substring(5,7)
          }
          if(this.dataForm.llKssj.substring(8,9)==0){
            dateOne=this.dataForm.llKssj.substring(9,10)
          }else{
            dateOne=this.dataForm.llKssj.substring(8,10)
          }
          if(this.dataForm.llJssj.substring(5,6)==0){
              monthTwo=this.dataForm.llJssj.substring(6,7)
          }else{
              monthTwo=this.dataForm.llJssj.substring(5,7)
          }
          if(this.dataForm.llJssj.substring(8,9)==0){
              dateTwo=this.dataForm.llJssj.substring(9,10)
          }else{
              dateTwo=this.dataForm.llJssj.substring(8,10)
          }
          if(yearOne==yearTwo){
            if(this.renzhi*1>28){
                
                if((dateOne<this.renzhi*1||dateOne==this.renzhi*1)&&(dateTwo>this.renzhi*1||dateTwo==this.renzhi*1||(monthTwo==2&&dateTwo==28)||(this.renzhi==31&&dateTwo==30&&(monthTwo==4||monthTwo==6||monthTwo==9||monthTwo==11)))){
                 
                  this.dataForm.jjFfMonth=monthTwo*1-monthOne*1+1
                }
                if((dateOne<this.renzhi*1||dateOne==this.renzhi*1)&&!(dateTwo>this.renzhi*1||dateTwo==this.renzhi*1||(monthTwo==2&&dateTwo==28)||(this.renzhi==31&&dateTwo==30&&(monthTwo==4||monthTwo==6||monthTwo==9||monthTwo==11)))){
                
                  this.dataForm.jjFfMonth=monthTwo*1-monthOne*1
                }
                if(dateOne>this.renzhi*1&&(dateTwo>this.renzhi*1||dateTwo==this.renzhi*1||(monthTwo==2&&dateTwo==28)||(this.renzhi==31&&dateTwo==30&&(monthTwo==4||monthTwo==6||monthTwo==9||monthTwo==11)))){
                  this.dataForm.jjFfMonth=monthTwo*1-monthOne*1
                }
                if(dateOne>this.renzhi*1&&!(dateTwo>this.renzhi*1||dateTwo==this.renzhi*1||(monthTwo==2&&dateTwo==28)||(this.renzhi==31&&dateTwo==30&&(monthTwo==4||monthTwo==6||monthTwo==9||monthTwo==11)))){
                 
                  this.dataForm.jjFfMonth=monthTwo*1-monthOne*1-1
                }
            }else{
              if((dateOne<this.renzhi*1||dateOne==this.renzhi*1)&&(dateTwo>this.renzhi*1||dateTwo==this.renzhi*1)){
                this.dataForm.jjFfMonth=monthTwo*1-monthOne*1+1
              }
              if((dateOne>this.renzhi*1&&(dateTwo>this.renzhi*1||dateTwo==this.renzhi*1))||((dateOne<this.renzhi*1||dateOne==this.renzhi*1)&&(dateTwo<this.renzhi*1))){
                this.dataForm.jjFfMonth=monthTwo*1-monthOne*1
              }
              if(dateOne>this.renzhi*1&&dateTwo<this.renzhi*1){
                this.dataForm.jjFfMonth=monthTwo*1-monthOne*1-1
              }
            }
            
         
          }
          if(yearTwo>yearOne){
            if(this.renzhi*1>28){
              if((dateOne<this.renzhi*1||dateOne==this.renzhi*1)&&(dateTwo>this.renzhi*1||dateTwo==this.renzhi*1||(monthTwo==2&&dateTwo==28)||(this.renzhi==31&&dateTwo==30&&(monthTwo==4||monthTwo==6||monthTwo==9||monthTwo==11)))){
                 
                  this.dataForm.jjFfMonth=monthTwo*1+(12-monthOne*1)+1
                }
                if((dateOne<this.renzhi*1||dateOne==this.renzhi*1)&&!(dateTwo>this.renzhi*1||dateTwo==this.renzhi*1||(monthTwo==2&&dateTwo==28)||(this.renzhi==31&&dateTwo==30&&(monthTwo==4||monthTwo==6||monthTwo==9||monthTwo==11)))){
                
                  this.dataForm.jjFfMonth=monthTwo*1+(12-monthOne*1)
                }
                if(dateOne>this.renzhi*1&&(dateTwo>this.renzhi*1||dateTwo==this.renzhi*1||(monthTwo==2&&dateTwo==28)||(this.renzhi==31&&dateTwo==30&&(monthTwo==4||monthTwo==6||monthTwo==9||monthTwo==11)))){
                  this.dataForm.jjFfMonth=monthTwo*1+(12-monthOne*1)
                }
                if(dateOne>this.renzhi*1&&!(dateTwo>this.renzhi*1||dateTwo==this.renzhi*1||(monthTwo==2&&dateTwo==28)||(this.renzhi==31&&dateTwo==30&&(monthTwo==4||monthTwo==6||monthTwo==9||monthTwo==11)))){
                 
                  this.dataForm.jjFfMonth=monthTwo*1+(12-monthOne*1)-1
                }
            }else{
              if((dateOne<this.renzhi*1||dateOne==this.renzhi*1)&&(dateTwo>this.renzhi*1||dateTwo==this.renzhi*1)){
                this.dataForm.jjFfMonth=monthTwo*1+(12-monthOne*1)+1
              }
              if((dateOne>this.renzhi*1)&&dateTwo<this.renzhi*1){
                this.dataForm.jjFfMonth=monthTwo*1+(12-monthOne*1)-1
              }
              if((dateOne>this.renzhi*1&&(dateTwo>this.renzhi*1||dateTwo==this.renzhi*1))||((dateOne<this.renzhi*1||dateOne==this.renzhi*1)&&dateTwo<this.renzhi*1)){
                this.dataForm.jjFfMonth=monthTwo*1+(12-monthOne*1)
              }
            }
            
            if((yearTwo*1-yearOne*1)>1){
              this.dataForm.jjFfMonth=this.dataForm.jjFfMonth+12*(yearTwo*1-yearOne*1-1)
            }
            this.dataForm.jjFfMonth=this.dataForm.jjFfMonth>12?12:this.dataForm.jjFfMonth
          }
        }
      },
      elChange(value){
        if(value === ''){
          this.ryXzjb = "";
          return;
        }
        for(let item of this.userZjOptions){
          if(item.value == value){
            this.dataForm.ryXzjb = item.label;
            this.dataForm.ryXzjbBh = item.value;
            break;
          }
        }
        this.$message.closeAll()
        if(!this.dataForm.deptId){
         this.$message.warning("请选择工作单位!");
         this.$set(this.dataForm,'ryXzjbBh','')
         return false
       }
        this.fnreturnDouble()
      },
      elChangeTwo(value){
        if(value === ''){
          this.ryXzjb = "";
          return;
        }
        this.$set(this.dataForm,'ryXzjb',value)
        this.$message.closeAll()
        if(!this.dataForm.deptId){
         this.$message.warning("请选择工作单位!");
         this.$set(this.dataForm,'ryXzjbBh','')
         return false
       }
        this.fnreturnDouble()
      },
      fnreturnDouble(){
        returnDouble(this.dataForm.deptId,this.dataForm.ryXzjb).then(res=>{
         
          if(res.data.data||res.data.data==0){
            if(this.dataForm.ryXzjb.includes('正')){
              this.$set(this.dataForm,'jxNxjs',res.data.data)
            }
            if(this.dataForm.ryXzjb.includes('副')){
              this.$set(this.dataForm,'fzNxbz',res.data.data)
            }
          }
        })
      },
      getDicCodeByName(strName){
        let lResult = "";
        this.userZjOptions.find(u =>{
          if(u.label.trim() === strName.trim()){
            lResult = u.value;
          }
        })
        return lResult;
      },
      init(id,timeForm) {

        this.dataForm.id = id || 0;
        this.visible = true;
        this.canSubmit = true;
        if(timeForm){
          if(timeForm.rzyfrq){
            this.renzhi=timeForm.rzyfrq
          }else{
            this.renzhi=1
          }
        }
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              this.dataForm = response.data.data

              this.dataForm.llYear = this.dataForm.llYear + '';
              this.dataForm.ryGzdwId = this.dataForm.deptId;
              this.$set(this.dataForm,"ryXzjbBh",this.getDicCodeByName(this.dataForm.ryXzjb))
            })
          }
        })
      },

      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {

            //不要删除
            //getJiangjinRyxxbPerPage({id: 0}).then((res)=>{})

            this.dataForm["rglrBs"] = 1;


            this.canSubmit = false;
            if (this.dataForm.id) {
              putObj(this.dataForm).then(data => {
                this.$notify.closeAll();
                this.$notify.success('修改成功')
                this.clearDataList();
                this.$emit('refreshDataListEdit')
              }).catch(() => {
                this.canSubmit = true;
              });
            } else {
              addObj(this.dataForm).then(data => {
                this.$notify.closeAll();
                this.$notify.success('添加成功')
                this.clearDataList();
                this.$emit('refreshDataList')
              }).catch(() => {
                this.canSubmit = true;
              });
            }
          }
        })
      },
      getNodeDataGzdw(data,node){
        ////console.log(data);
        ////console.log(data.$parent);

        this.dataForm.ryGzdw = data.deptName;
        this.dataForm["ryGzdwCode"] = data.deptCode;
        // this.dataForm.ryGzdwId = data.id;
        this.dataForm.deptId = data.id;
      //  console.log(this.dataForm.ryGzdwId)
       // console.log(this.dataForm.deptId)
    
      },
      //奖金发放部门
      getNodeDatajjFfBm(data,node){
        this.dataForm.jjFfDeptName = data.deptName;
        this.dataForm["jjFfDeptCode"] = data.deptCode;
        this.dataForm.jjFfDeptId = data.id;
      },

      // 取得角色用户-部门日常考核
      getUserCheck(data){
        if(data.length === 0){

          this.dataForm.ryXm = "";
          this.dataForm.ryCode = "";
        }else {
          const c1= this.getStrJoin("ryXm",data);
          const c2= this.getStrJoin("ryCode",data);

          this.dataForm.ryCode =  c2.join(',');
          this.dataForm.ryXm = c1.join(',');
        }
      },
      //拼接字符串
      getStrJoin(strKey,strData){
        let lTemp = [];
        strData.map(item => {
          lTemp.push(item[strKey]);
        })
        return lTemp;
      },
      selectCheckUser(){

        this.roleCode = "";
        this.checkUserIds = [];
        this.checkUserIds.push(this.getBlankValue(this.dataForm.ryCode));
        // this.checkUserIds = ["00380359","00392362","00397655"];

        //防止数据慢于页面加载
        this.$nextTick(()=>{
          this.$refs.checkUser.init();
        })
      },
      //取得空值 增加弹错判断
      getBlankValue(value){
        let lResult = "";
        if(value === undefined || value === null){
        }else {
          lResult = value;
        }
        return lResult;
      },

      clearDataList(){
        this.dataForm ={
            id: 0,
            ryCode: '',
            ryXm: '',
            llKssj: '',
            zwjb:"",
            llJssj: '',
            ryGzdwId:'',
            ryGzdw: '',
            ryGzdwCode: '',
            ryZwgz: '',
            ryXzjb: '',
            ryXzjbBh:'',
            ryJszc: '',
            ryJnjb: '',
            llBz: '',
            llYear:'',
            jjFfDeptId:'',
            jjFfDeptCode:'',
            jjFfDeptName:'',
            jjFfMonth:'',
            rglrBs:'',
            ryXXData:[],
            deptId:''
        };
        this.canSubmit = true;
        this.visible = false;
      },

    }
  }

</script>
